package com.ekingstar.hedss.action;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletInputStream;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import oracle.sql.BLOB;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.config.Action;
import org.apache.struts2.config.Result;
import org.apache.struts2.config.Results;
import org.apache.struts2.dispatcher.ServletActionRedirectResult;
import org.hibernate.Hibernate;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.ekingstar.framework.domain.User;
import com.ekingstar.framework.security.UserUtil;
import com.ekingstar.framework.util.Page;
import com.ekingstar.hedss.domain.HedssZlfj;
import com.ekingstar.hedss.domain.HedssZlxx;
import com.ekingstar.hedss.service.IHedssService;

@Action(name = "upload", namespace = "/upload")
@Results( {
	@Result(name = "input", value = "/pages/documentation/zlfj-upload.jsp"),
	@Result(name = "uploadsuccess",value="documentation!viewZlxx.action",type=ServletActionRedirectResult.class,params={"namespace","/documentation","id","${zlxx.id}","searchzlxx.TZllb.id","${zlxx.TZllb.id}"}),
	@Result(name = "viewzlxx",value="documentation!viewZlxx.action",type=ServletActionRedirectResult.class,params={"namespace","/documentation","id","${zlxx.id}","searchzlxx.TZllb.id","${zlxx.TZllb.id}"}),
	@Result(name = "nologin", value = "/user/nologin.jsp")})
public class UploadAction extends ActionSupport {		
	private static final long serialVersionUID = 1L;
	private File file;
	private String filename;
	private HedssZlfj zlfj;
	private HedssZlxx zlxx;
	private List haszlfj;
	private IHedssService hedssService;
	
	public String input(){
				
		return "input";
	}	
	
	public String execute(){
		boolean logged = UserUtil.isLogged();
		if (!logged ){
			return "nologin";
		}
		User user= UserUtil.getCurrentUser();
				
		try {
			
			if(file!=null){
				if(file.length()>5000000){
					this.addFieldError("file","上传文件大小限制为5M。");
					return "input";
				}
				
				String fjid=hedssService.saveFile(file,filename,zlfj.getTZlxx().getId(),user.getName());
				
				if(fjid!=null){					
					zlxx=hedssService.getZlxx(zlfj.getTZlxx().getId());
				}
			}
			
		}catch(Exception e){
			e.printStackTrace();
		}
		
		return "uploadsuccess";
	}	
	

	public String backtoviewZlxx(){
		this.zlxx = hedssService.viewZlxx(zlfj.getTZlxx().getId());
		this.haszlfj = hedssService.getZlfjByZlxxId(this.zlxx.getId());
		return "viewzlxx";
	}
			
	public File getFile() {
		return file;
	}

	public void setFile(File file) {
		this.file = file;
	}	
	
	public String getFilename() {
		return filename;
	}

	public void setFilename(String filename) {
		this.filename = filename;
	}

	public HedssZlfj getZlfj() {
		return zlfj;
	}

	public void setZlfj(HedssZlfj zlfj) {
		this.zlfj = zlfj;
	}

	public IHedssService getHedssService() {
		return hedssService;
	}

	public void setHedssService(IHedssService hedssService) {
		this.hedssService = hedssService;
	}

	public HedssZlxx getZlxx() {
		return zlxx;
	}

	public void setZlxx(HedssZlxx zlxx) {
		this.zlxx = zlxx;
	}

	public List getHaszlfj() {
		return haszlfj;
	}

	public void setHaszlfj(List haszlfj) {
		this.haszlfj = haszlfj;
	}

	
}
